<?php

/**
 * This is the model class for table "{{city}}".
 *
 * The followings are the available columns in table '{{city}}':
 * @property integer $cityID
 * @property integer $stateID
 * @property string $city_name
 * @property string $created
 * @property string $modified
 */
class City extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return City the static model class
	 */
	public $countryID;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{city}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('stateID,city_name', 'required'),
			array('stateID', 'numerical', 'integerOnly'=> true),
			array('city_name', 'length', 'max'=>300),
			array('city_name', 'checkCityName', 'on'=>'insert'),
			array('created, modified', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('cityID, stateID, city_name, created, modified', 'safe', 'on'=>'search'),
			array('modified','default',
           	  'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'update'),
        	array('created,modified','default',
              'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'insert'),			
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'cityID' => 'ID',
			'stateID' => 'STATE NAME',
			'city_name' => 'CITY NAME',
			'created' => 'CREATED',
			'modified' => 'MODIFIED',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$state = null;
		if (isset($this->stateID) && $this->stateID != null)
		{
			$criteria=new CDbCriteria;
			$criteria->select = 'stateID' ;
			$criteria->condition = 'state_name LIKE :name' ;
			$criteria->params = array(':name'=>'%'.$this->stateID.'%') ;
			$state = State::model()->find($criteria);
		}
		
		$criteria=new CDbCriteria;

		$criteria->compare('cityID',$this->cityID);
		
		if (!empty($state))
		{
			$criteria->compare('stateID',$state->stateID);
		}
		
		$criteria->compare('city_name',$this->city_name,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);
		
		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	
	public static function getList($type,$arrParams)
	{
		switch ($type)
		{
			case 'CITY-LIST':
				$arrCityList = City::model()->findAll(array('order'=>'city_name'));
												
				foreach ($arrCityList as $city)
				{
					$AllCityList[$city->cityID] = $city->city_name;
				}
				$arrtopcitites = ApplicationConfig::app()->params['scaling_params']['top-cities'];
				foreach ($arrtopcitites as $cityid)
             	{
             		$AllCityList = ApplicationConfig::ArryMovetoTop($AllCityList, $cityid);
             	}
				
				
			return $AllCityList;
			break;
			case 'CITY-LIST-LIKE':
				$AllStateList = array();
				if (empty($arrParams['stateID']))
				$arrCityList = City::model()->findAll('city_name LIKE :qterm', array(':qterm'=>$arrParams['qterm']));
				else
				$arrCityList = City::model()->findAll('city_name LIKE :qterm AND stateID = :stateID',array(':qterm'=>$arrParams['qterm'],':stateID'=>$arrParams['stateID']));
				foreach ($arrCityList as $state)
				{
					$AllStateList[$state->cityID] = $state->city_name;
				}
			return $AllStateList;
			break;
			case 'BY-STATE-ID':
			return City::model()->findAll('stateID=:stateID',array(':stateID'=>$arrParams['stateID']));
			break;
		}
	}
	
	
	public function getModel($type,$arrParams)
	{
		switch ($type)
		{
			case 'BY-STATE-ID':
			return City::model()->find('stateID=:stateID',array(':stateID'=>$arrParams['stateid']));
			break;
			
			case 'CITY-LIKE':
			 	$cityobj = City::model()->find(array(
					'condition' => "city_name LIKE :match",      
					'params'    => array(':match' => "%".$arrParams['city_name']."%")
				));
			return $cityobj;
			break;	
			
			case 'BY-ID':
			return City::model()->findByPk((int)$arrParams['id']);
			break;
		}
	}
	
	public function checkCityName()
	{
		$found = City::model()->find('city_name=? and stateID=?',array($this->city_name,$this->stateID));
		if($found!=null)
			$this->addError('name','Same city for given state already exists!');
  	}

  	public function getCityNameByID($id)
	{
		$model = City::model()->findByPk((int)$id);
		return $model->city_name;
	}
	
	/* Gives list of cities */
	public function getCity()
	{
		$arrcity = array();
		$models = City::model()->findAll();
		foreach($models as $model){
			$arrcity[$model->cityID]=$model->city_name;
		}
		 return $arrcity;
	}
  	
}